﻿unit SysRightRegisterService;

interface
{$I ../Config.inc}
uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SKSOFTComm,
  SysRightRegisterModel,
  SysRightRegisterBLL;
  
type


  TSysRightRegisterService = class(TSysRightRegisterBLL)
  private

  protected

  public
  {$IFDEF SERVER}
    function BeforeTreatment(const iItem:TSysRightRegister; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeTreatment(const iItems:TSysRightRegisterList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeCheck(const iItem:TSysRightRegister; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeCheck(const iItems:TSysRightRegisterList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function Check(const iItem:TSysRightRegister; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function Check(const iItems:TSysRightRegisterList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeExecute(const iItem:TSysRightRegister; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeExecute(const iItems:TSysRightRegisterList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function AfterTreatment(const iItem:TSysRightRegister; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function AfterTreatment(const iItems:TSysRightRegisterList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function CalculateIdAndSortNo(const iPid:string; out oNewSortNo:Integer; out oNewId,oError:string):Boolean;
  {$ENDIF}

    function LoadAll(const iItems:TSysRightRegisterList; out oError:string; const iJustUse:Boolean=False):Boolean;
    function ExchangeSortNo(const iItem1:TSysRightRegister; const iItem2:TSysRightRegister; out oError:string):Boolean;
    function Use(const iItem:TSysRightRegister; out oError:string):Boolean;

    function LoadManageComponentRight(const iManageId:string; const iItems:TSysRightRegisterList; out oError:string; const iJustUse:Boolean=False):Boolean;

    class procedure RightControlStr2Var(const iRightControlStr:string; out oComponentClassName:string; out oComponentName:string; out oControlType:string; out oInvokePropertyName:string);
    class procedure Var2RightControlStr(const iComponentClassName:string; const iComponentName:string; const iControlType:string; out oRightControlStr:string);
  end;
  
implementation

{$IFDEF SERVER}
function TSysRightRegisterService.BeforeTreatment(const iItem:TSysRightRegister; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  ItemDB:TSysRightRegister;
  NewSortNo:Integer;
  NewId:string;
begin
  if iItem<>nil then
  begin
    ItemDB:=TSysRightRegister.Create;
    try
      case iPreMode of
      pmAdd:
        begin
          if not CalculateIdAndSortNo(iItem.RightTreePid,NewSortNo,NewId,oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
          iItem.RightId:=iItem.ObjGUID;
          iItem.RightTreeId:=NewId;
          iItem.RightIsSystem:=False;
          iItem.RightStatus:=1;
          iItem.RightSortNo:=NewSortNo;
          iItem.RecordDate:=Now;
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItem.ClassName)) then
          begin
            if ItemDB.LoadFromStr(iItem.SaveToStr,oError) and
              Reload(ItemDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItem.ClassName),ItemDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItem.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;

          iItem.ModifyDate:=Now;
        end;
      pmDelete:
        begin
          if not Reload(iItem,oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      end;
    finally
      FreeAndNil(ItemDB);
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeTreatment错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegisterService.BeforeTreatment(const iItems:TSysRightRegisterList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  Key:string;
  ItemsDB:TSysRightRegisterList;
begin
  if iItems<>nil then
  begin
    ItemsDB:=TSysRightRegisterList.Create;
    try
      case iPreMode of
      pmAdd:
        begin
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItems.ClassName)) then
          begin
            if ItemsDB.LoadFromStr(iItems.SaveToStr,oError) and
              Reload(ItemsDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItems.ClassName),ItemsDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemsDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItems.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
          if not Reload(iItems,oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      end;
      for Key in iItems.SortKeys do
      begin
        if iPreMode=pmEdit then
        begin
          iParcel.PutStringGoods(AnsiString(iItems.Items[Key].ClassName),ItemsDB.Items[Key].SaveToStr);
        end;
        if not BeforeTreatment(iItems.Items[Key],iPreMode,iParcel,oError) then
        begin
          oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end;
    finally
      FreeAndNil(ItemsDB);
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeTreatment错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegisterService.BeforeCheck(const iItem:TSysRightRegister; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRightRegisterService.BeforeCheck(const iItems:TSysRightRegisterList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRightRegisterService.Check(const iItem:TSysRightRegister; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  ItemDB:TSysRightRegister;
begin
  if iItem<>nil then
  begin
    ItemDB:=TSysRightRegister.Create;
    try
      if SameStr(Trim(iItem.RightId),EmptyStr) then
      begin
        oError:='注册模块ID不允许为空';
        Result:=False;
        Exit;
      end else if SameStr(Trim(iItem.RightTreeId),EmptyStr) then
      begin
        oError:='注册模块树ID不允许为空';
        Result:=False;
        Exit;
      end else if SameStr(Trim(iItem.RightTreePid),EmptyStr) then
      begin
        oError:='注册模块树PID不允许为空';
        Result:=False;
        Exit;
      end else if iItem.RightIsSystem then
      begin
        oError:='不允许创建、修改、删除系统级注册模块';
        Result:=False;
        Exit;
      end else if (iItem.RightStatus<0) or (iItem.RightStatus>1) then
      begin
        oError:='注册模块状态非法';
        Result:=False;
        Exit;
      end;

      case iPreMode of
      pmAdd:
        begin
          if SameStr(Trim(iItem.RecordPsn),EmptyStr) then
          begin
            oError:='录入人不允许为空';
            Result:=False;
            Exit;
          end;
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItem.ClassName)) then
          begin
            if ItemDB.LoadFromStr(iItem.SaveToStr,oError) and
              Reload(ItemDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItem.ClassName),ItemDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItem.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
          if SameStr(Trim(iItem.ModifyPsn),EmptyStr) then
          begin
            oError:='修改人不允许为空';
            Result:=False;
            Exit;
          end else if iItem.RightId<>ItemDB.RightId then
          begin
            oError:='注册模块ID不允许修改';
            Result:=False;
            Exit;
          end else if iItem.RightTreeId<>ItemDB.RightTreeId then
          begin
            oError:='注册模块树ID不允许修改';
            Result:=False;
            Exit;
          end else if iItem.RightTreePid<>ItemDB.RightTreePid then
          begin
            oError:='注册模块树PID不允许修改';
            Result:=False;
            Exit;
          end else if iItem.RightIsSystem<>ItemDB.RightIsSystem then
          begin
            oError:='注册模块类型不允许修改';
            Result:=False;
            Exit;
          end else if not SameStr(Trim(iItem.RecordPsn),Trim(ItemDB.RecordPsn)) then
          begin
            oError:='录入人不允许修改';
            Result:=False;
            Exit;
          end else if not SameDateTime(iItem.RecordDate,ItemDB.RecordDate) then
          begin
            oError:='录入日期不允许修改';
            Result:=False;
            Exit;
          end else if iItem.TimeStamp<>ItemDB.TimeStamp then
          begin
            oError:='注册模块信息已被他人修改，请重新装载...';
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
          if iItem.RightStatus<>0 then
          begin
            oError:='只允许删除已冻结注册模块';
            Result:=False;
            Exit;
          end
        end;
      end;
    finally
      FreeAndNil(ItemDB);
    end;
  end else begin
    oError:=Format(string('调用%s.Check错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegisterService.Check(const iItems:TSysRightRegisterList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  Key:string;
  ItemsDB:TSysRightRegisterList;
begin
  if iItems<>nil then
  begin
    ItemsDB:=TSysRightRegisterList.Create;
    try
      case iPreMode of
      pmAdd:
        begin
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItems.ClassName)) then
          begin
            if ItemsDB.LoadFromStr(iItems.SaveToStr,oError) and
              Reload(ItemsDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItems.ClassName),ItemsDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemsDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItems.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
        end;
      end;
      for Key in iItems.SortKeys do
      begin
        if iPreMode=pmEdit then
        begin
          iParcel.PutStringGoods(AnsiString(iItems.Items[Key].ClassName),ItemsDB.Items[Key].SaveToStr);
        end;
        if not Check(iItems.Items[Key],iPreMode,iParcel,oError) then
        begin
          oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end;
    finally
      FreeAndNil(ItemsDB);
    end;
  end else begin
    oError:=Format(string('调用%s.Check错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegisterService.BeforeExecute(const iItem:TSysRightRegister; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  if iItem<>nil then
  begin
    case iPreMode of
    pmAdd:
      begin
        iItem.TimeStamp:=TSKSOFTComm.GetTimeStampMs;
      end;
    pmEdit:
      begin
        iItem.TimeStamp:=TSKSOFTComm.GetTimeStampMs;
      end;
    pmDelete:
      begin
      end;
    pmUse:
      begin
      end;
    pmUnUse:
      begin
      end;
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeExecute错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegisterService.BeforeExecute(const iItems:TSysRightRegisterList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  Key:string;
  ItemsDB:TSysRightRegisterList;
begin
  if iItems<>nil then
  begin
    ItemsDB:=TSysRightRegisterList.Create;
    try
      case iPreMode of
      pmAdd:
        begin
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItems.ClassName)) then
          begin
            if ItemsDB.LoadFromStr(iItems.SaveToStr,oError) and
              Reload(ItemsDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItems.ClassName),ItemsDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.BeforeExecute错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemsDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItems.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.BeforeExecute错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
        end;
      end;
      for Key in iItems.SortKeys do
      begin
        if iPreMode=pmEdit then
        begin
          iParcel.PutStringGoods(AnsiString(iItems.Items[Key].ClassName),ItemsDB.Items[Key].SaveToStr);
        end;
        if not BeforeCheck(iItems.Items[Key],iPreMode,iParcel,oError) then
        begin
          oError:=Format(string('调用%s.BeforeExecute错误 --> %s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end;
    finally
      FreeAndNil(ItemsDB);
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeExecute错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegisterService.AfterTreatment(const iItem:TSysRightRegister; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRightRegisterService.AfterTreatment(const iItems:TSysRightRegisterList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysRightRegisterService.CalculateIdAndSortNo(const iPid:string; out oNewSortNo:Integer; out oNewId,oError:string):Boolean;
var
  SQL,MaxId,NumStr,MaxSortNoStr:string;
  NewNum:Integer;
begin
  if SameStr(Trim(iPid),EmptyStr) then
  begin
    oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败, 错误内容:传入的iPid为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;

  SQL:=Format(string('SELECT MAX(%s) AS %s FROM %s WHERE %s=''%s'';'),
          [TSysRightRegisterModel.cRightTreeIdKey,TSysRightRegisterModel.cRightTreeIdKey,
              TSysRightRegisterModel.cTblName,TSysRightRegisterModel.cRightTreePidKey,
              iPid]);
  if TSKSOFTComm.ReadSimpleData(fServConn,EmptyStr,SQL,MaxId,oError) then
  begin
    if SameStr(Trim(MaxId),EmptyStr) then
    begin
      oNewId :=iPid+'.001';
    end else begin
      NumStr:=string(TSKSOFTComm.AfterStr(AnsiString(MaxId),AnsiString(iPid+'.')));
      if Length(NumStr)=3 then
      begin
        NewNum:=StrToInt(NumStr[1])*100+StrToInt(NumStr[2])*10+StrToInt(NumStr[3])+1;
        if NewNum<10 then
        begin
          oNewId :=iPid+'.00'+IntToStr(NewNum);
        end else if NewNum<100 then
        begin
          oNewId :=iPid+'.0'+IntToStr(NewNum);
        end else if NewNum<1000 then
        begin
          oNewId :=iPid+'.'+IntToStr(NewNum);
        end else begin
          oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败,错误内容:新Id=1000 越界溢出!'),[Self.ClassName]);
          Result:=False;
          Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败,错误内容:截取的id尾数字符串长度不等于3!'),[Self.ClassName]);
        Result:=False;
        Exit;
      end;
    end;
  end else begin
    oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败,错误内容:获取最大同级Id失败!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;

  SQL:=Format(string('SELECT MAX(%s) AS %s FROM %s WHERE %s=''%s'';'),
          [TSysRightRegisterModel.cRightSortNoKey,TSysRightRegisterModel.cRightSortNoKey,
              TSysRightRegisterModel.cTblName,TSysRightRegisterModel.cRightTreePidKey,
              iPid]);
  if TSKSOFTComm.ReadSimpleData(fServConn,EmptyStr,SQL,MaxSortNoStr,oError) then
  begin
    if SameStr(Trim(MaxSortNoStr),EmptyStr) then
    begin
      oNewSortNo:=1;
    end else begin
      oNewSortNo:=StrToInt(MaxSortNoStr)+1;
    end;
  end else begin
    oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败, 错误内容:获取最大同级SortNo失败!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;
{$ENDIF}

function TSysRightRegisterService.LoadAll(const iItems:TSysRightRegisterList; out oError:string; const iJustUse:Boolean):Boolean;
var
  Condition,OrderBy:string;
begin
  if (iItems<>nil) then
  begin
    if iJustUse then
    begin
      Condition:=Format(string('%s=1'),[TSysRightRegisterModel.cRightStatusKey]);
    end else begin
      Condition:='1=1';
    end;
    OrderBy:=TSysRightRegisterModel.cRightTreePidKey+','+TSysRightRegisterModel.cRightSortNoKey+' ASC';

    if not LoadByCondition(iItems,Condition,oError,OrderBy) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadAll方法失败！错误内容:传入的iItems为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegisterService.ExchangeSortNo(const iItem1,iItem2:TSysRightRegister; out oError:string):Boolean;
var
  TempSortNo:Integer;
  RightList:TSysRightRegisterList;
  NewItem1,NewItem2:TSysRightRegister;
  Cds:TClientDataSet;
begin
  if (iItem1<>nil) and
    (iItem2<>nil) then
  begin
    RightList:=TSysRightRegisterList.Create;
    NewItem1:=TSysRightRegister.Create;
    NewItem2:=TSysRightRegister.Create;
    Cds:=TClientDataSet.Create(nil);
    try
      if GetCDSModel(Cds,oError) then
      begin
        if iItem1.SaveToCDS(Cds,oError) and
          NewItem1.LoadFromCDS(Cds,oError) and
          iItem2.SaveToCDS(Cds,oError) and
          NewItem2.LoadFromCDS(Cds,oError) then
        begin
          TempSortNo:=iItem1.RightSortNo;
          NewItem1.RightSortNo:=iItem2.RightSortNo;
          NewItem2.RightSortNo:=TempSortNo;

          RightList.Add(NewItem1);
          RightList.Add(NewItem2);
        end else begin
          oError:=Format(string('调用%s.ExchangeSortNo方法失败！错误内容%s:'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.ExchangeSortNo方法失败！错误内容%s:'),[Self.ClassName,oError]);
        Result:=False;
        Exit;
      end;
      if Inherited Edit(RightList,oError) then
      begin
        if iItem1.LoadFromStr(NewItem1.SaveToStr,oError) and
          iItem2.LoadFromStr(NewItem2.SaveToStr,oError) then
        begin

        end else begin
          oError:=Format(string('调用%s.ExchangeSortNo方法失败！错误内容%s:'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end;
    finally
      RightList.Free;
      Cds.Free;
    end;
  end else begin
    oError:=Format(string('调用%s.ExchangeSortNo方法失败！错误内容:传入的iItem1或者iItem2为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegisterService.Use(const iItem:TSysRightRegister; out oError:string):Boolean;
var
  Condition,Key:string;
  List:TSysRightRegisterList;
begin
  if (iItem<>nil) then
  begin
    if iItem.RightStatus=1 then
    begin
      Condition:=Format(string('%s LIKE ''%s'' '),[TSysRightRegisterModel.cRightTreeIdKey,iItem.RightTreeId+'%']);
      List:=TSysRightRegisterList.Create;
      try
        if not LoadByCondition(List,Condition,oError) then
        begin
          oError:=Format(string('调用%s.Use方法失败！错误内容:%s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
        for Key in List.SortKeys do
        begin
          List.Items[Key].RightStatus:=0;
        end;
        if not inherited Edit(List,oError) then
        begin
          oError:=Format(string('调用%s.Use方法失败！错误内容:%s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      finally
        List.Free;
      end;
    end else begin
      iItem.RightStatus:=1;
      if not inherited Edit(iItem,oError) then
      begin
        oError:=Format(string('调用%s.Use方法失败！错误内容:%s'),[Self.ClassName,oError]);
        Result:=False;
        Exit;
      end;
    end;
  end else begin
    oError:=Format(string('调用%s.Use方法失败！错误内容:传入的iItem为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysRightRegisterService.LoadManageComponentRight(const iManageId:string; const iItems:TSysRightRegisterList; out oError:string; const iJustUse:Boolean=False):Boolean;
var
  Condition,OrderBy:string;
begin
  if (not SameStr(Trim(iManageId),EmptyStr)) and (iItems<>nil) then
  begin
    Condition:=Format(string('%s=''%s'' and %s>%d '),
                      [TSysRightRegisterModel.cRightUiManageIdKey,iManageId,
                      TSysRightRegisterModel.cRightDeepKey,3]);//取菜单权限以后的数据
    if iJustUse then
    begin
      Condition:=Condition+Format(string(' and %s=%d '),
                                  [TSysRightRegisterModel.cRightStatusKey,1]);
    end;
    OrderBy:=TSysRightRegisterModel.cRightTreePidKey+','+TSysRightRegisterModel.cRightSortNoKey+' ASC';
    if not LoadByCondition(iItems,Condition,oError,OrderBy) then
    begin
      oError:=Format(string('调用%s.LoadManageComponentRight方法失败！错误内容:%s'),[Self.ClassName,oError]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadManageComponentRight方法失败！错误内容:传入的%s或%s为空'),[Self.ClassName,'iManageId','iManageFrame']);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

class procedure TSysRightRegisterService.RightControlStr2Var(const iRightControlStr:string; out oComponentClassName:string; out oComponentName:string; out oControlType:string; out oInvokePropertyName:string);
var
  Json:TQJson;
  ComponentClassNameKey,ComponentNameKey,ControlTypeKey,InvokePropertyNameKey:string;
begin
  Json:=TQJson.Create;
  ComponentClassNameKey:='ComponentClassName';
  ComponentNameKey:='ComponentName';
  ControlTypeKey:='ControlType';
  InvokePropertyNameKey:='InvokePropertyName';
  try
    if Json.TryParse(iRightControlStr) then
    begin
      if Json.ItemByName(ComponentClassNameKey)<>nil then
      begin
        oComponentClassName:=Json.ItemByName(ComponentClassNameKey).AsString;
      end else begin
        oComponentClassName:=EmptyStr;
      end;

      if Json.ItemByName(ComponentNameKey)<>nil then
      begin
        oComponentName:=Json.ItemByName(ComponentNameKey).AsString;
      end else begin
        oComponentName:=EmptyStr;
      end;

      if Json.ItemByName(ControlTypeKey)<>nil then
      begin
        oControlType:=Json.ItemByName(ControlTypeKey).AsString;
      end else begin
        oControlType:=EmptyStr;
      end;

      if Json.ItemByName(InvokePropertyNameKey)<>nil then
      begin
        oInvokePropertyName:=Json.ItemByName(InvokePropertyNameKey).AsString;
      end else begin
        oInvokePropertyName:=EmptyStr;
      end;

    end else begin
      oComponentClassName:=EmptyStr;
      oComponentName:=EmptyStr;
      oControlType:=EmptyStr;
      oInvokePropertyName:=EmptyStr;
    end;
  finally
    Json.Free;
  end;
end;

class procedure TSysRightRegisterService.Var2RightControlStr(const iComponentClassName:string; const iComponentName:string; const iControlType:string; out oRightControlStr:string);
var
  Json:TQJson;
  ComponentClassNameKey,ComponentNameKey,ControlTypeKey,InvokePropertyNameKey:string;
begin
  ComponentClassNameKey:='ComponentClassName';
  ComponentNameKey:='ComponentName';
  ControlTypeKey:='ControlType';
  InvokePropertyNameKey:='InvokePropertyName';
  Json:=TQJson.Create;
  try
    Json.Add(ComponentClassNameKey).AsString:=iComponentClassName;
    Json.Add(ComponentNameKey).AsString:=iComponentName;
    Json.Add(ControlTypeKey).AsString:=iControlType;
    if SameStr(iComponentClassName,'TRzToolButton') then
    begin
      if SameStr(iControlType,'Visible') then
      begin
        Json.Add(InvokePropertyNameKey).AsString:='Visible';
      end else if SameStr(iControlType,'Editable') then
      begin
        Json.Add(InvokePropertyNameKey).AsString:='Enabled';
      end else begin
        Json.Add(InvokePropertyNameKey).AsString:=EmptyStr;
      end;
    end else begin
      Json.Add(InvokePropertyNameKey).AsString:=EmptyStr;
    end;
    oRightControlStr:=Json.ToString;
  finally
    Json.Free;
  end;
end;
  
end.
